package com.dataart.training.crimequalification.beans;

import com.dataart.training.crimequalification.entity.UserEntity;
import com.dataart.training.crimequalification.facesutils.FacesMessageHandlingUtils;
import com.dataart.training.crimequalification.facesutils.ResourceBundleManager;
import com.dataart.training.crimequalification.security.PasswordEncoder;
import com.dataart.training.crimequalification.service.AuthenticationService;
import com.dataart.training.crimequalification.service.AuthenticationServiceImpl;
import com.dataart.training.crimequalification.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.authentication.dao.SaltSource;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import java.util.ResourceBundle;

@ManagedBean
@Controller
@Scope(value = "request")
public class UserDetailsBean {

    // --------------------------------------------------------------------------

    private static final Logger logger = LoggerFactory
            .getLogger(UserDetailsBean.class);

    // --------------------------------------------------------------------------

    @Autowired
    private UserService userService;

    @Autowired
    private SaltSource saltSource;

    // --------------------------------------------------------------------------

    private UserEntity curUser;
    private UserEntity changedUser;

    private String newPassword;
    private String newPasswordConfirmation;

    // --------------------------------------------------------------------------

    @PostConstruct
    private void init() {

        AuthenticationService authUtils = new AuthenticationServiceImpl();
        curUser = authUtils.getCurrentLoggedInUser(userService);

        if (curUser != null) {

            changedUser = new UserEntity();
            changedUser.setEmail(curUser.getEmail());
            changedUser.setEnabled(curUser.getEnabled());
            changedUser.setName(curUser.getName());
            changedUser.setPassword(curUser.getPassword());
            changedUser.setPhoneNumber(curUser.getPhoneNumber());
            changedUser.setUserID(curUser.getUserID());
            changedUser.setUsername(curUser.getUsername());
            changedUser.setVerified(curUser.isVerified());

        }
    }

    // --------------------------------------------------------------------------

    public UserEntity getCurUser() {
        return curUser;
    }

    public UserEntity getChangedUser() {
        return changedUser;
    }

    public void setChangedUser(UserEntity changedUser) {
        this.changedUser = changedUser;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String getNewPasswordConfirmation() {
        return newPasswordConfirmation;
    }

    public void setNewPasswordConfirmation(String newPasswordConfirmation) {
        this.newPasswordConfirmation = newPasswordConfirmation;
    }

    // --------------------------------------------------------------------------

    public String editUserInfo() {

        logger.info("Editing user information");
        logger.debug(changedUser.getUsername());
        userService.updateUserDetails(changedUser);

        return "/views/index?faces-redirect=true";

    }

    // --------------------------------------------------------------------------

    public void editUserPassword() {

        ResourceBundleManager resourceBundleManager = new ResourceBundleManager();
        ResourceBundle msgsBundle = resourceBundleManager.getResourceBundleByName("msgs");
        String message;

        PasswordEncoder passwordEncoder = new PasswordEncoder();
        changedUser.setPassword(passwordEncoder.encodePassword(changedUser.getPassword(),
                saltSource.getSalt(userService.userEntityToUserDetails(changedUser))));

        if (!(this.curUser.getPassword().equals(this.changedUser.getPassword()))) {

            message = resourceBundleManager.getValueFromBundleByKey(msgsBundle,
                    "editdetails.error.incorrectcurpassword");


        } else if (!(this.newPassword.equals(this.newPasswordConfirmation))) {

            message = resourceBundleManager.getValueFromBundleByKey(msgsBundle,
                    "editdetails.error.newpasswordmismatch");

        } else {

            this.changedUser.setPassword(passwordEncoder.encodePassword(newPassword,
                    saltSource.getSalt(userService.userEntityToUserDetails(changedUser))));
            userService.updateUserDetails(changedUser);
            message = resourceBundleManager.getValueFromBundleByKey(msgsBundle,
                    "editdetails.success.editsuccess");

        }

        FacesMessageHandlingUtils facesMessageHandlingUtils = new FacesMessageHandlingUtils();
        facesMessageHandlingUtils.addMessageToComponent("changePasswordForm", message + ".");

    }

    // --------------------------------------------------------------------------
}
